﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Subtle.ViewModels;
using Subtle.Domain.Services.Interfaces;

namespace Subtle.Controllers
{
	/// <summary>
	/// Home Controller
	/// </summary>
    public class HomeController : BaseController
    {
		private IPostService postService;
		private IBlogService blogService;

		/// <summary>
		/// Initializes a Home Controller
		/// </summary>
		/// <param name="postService"></param>
		/// <param name="blogService"></param>
		public HomeController(IPostService postService, IBlogService blogService)
		{
			this.postService = postService;
			this.blogService = blogService;
		}

		/// <summary>
		/// Home/Index
		/// </summary>
		/// <returns></returns>
		public ActionResult Index()
		{
			//ToDo : Configuration at this point is really ugly, need to come up with a cleaner solution
			if (!blogService.HasBlogs)
			{
				return RedirectToAction("ConfigureUser", "Blog");
			}

			IList<HomePostViewModel> homePostViewModel = (from post in postService.GetLatestPublishedPosts(5)
														  select HomePostViewModel.GetModel(post)).ToList<HomePostViewModel>();
			return View(homePostViewModel);
		}

		/// <summary>
		/// Home/Index
		/// </summary>
		/// <returns></returns>
		public ActionResult Archives()
		{
			IList<HomeArchiveViewModel> homeArchiveViewModel = (from archiveItem in postService.GetArchives()
																orderby archiveItem.Key descending
																select HomeArchiveViewModel.GetModel(archiveItem)).ToList<HomeArchiveViewModel>();
			return Json(homeArchiveViewModel, JsonRequestBehavior.AllowGet);
		}
    }
}
